package boot.spring.service.impl;

import boot.spring.entity.Users;
import boot.spring.mapper.UserInfoMapper;
import boot.spring.mapper.UsersMapper;
import boot.spring.service.UsersService;
import boot.spring.util.MD5Utils;
import boot.spring.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UsersServiceImpl implements UsersService {
    //引用Usersmapper持久层
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    //获取当前时间
    Date date = new Date();
    SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
    //把当前时间的类型转换成字符串
    String datetime=dateFormat.format(date)+"";
    //创建返回值数据类型的对象
    Result r=new Result();

    @Override /*分页获取所有数据  根据多条件(用户名、起止时间、是否锁定、排序)*/
    public Result UserSerAll(Integer page,Integer limit,
                                  String uname,String begintime,
                                  String endtime,String iflock,
                                  String weight) {
        Map<String ,Object> all = new HashMap<>();
        all.put("page",(page-1)*limit);
        all.put("limit",limit);
        all.put("uname",uname);
        all.put("begintime",begintime);
        all.put("endtime",endtime);
        all.put("iflock",iflock);
        all.put("weight",weight);
        List<Map<String,Object>> users=usersMapper.UserMapAll(all);
        Integer count=usersMapper.countMap(all);
        if (users.size()>0){
            r=new Result(0,"请求成功",count,users);
        }else{
            r=new Result(0,"请求失败");
        }
        return r;
    }

   /* @Override*//*获取数据的条数*//*
    public Integer countSet(Integer page,Integer limit,String uname,String begintime,String endtime,String iflock,String weight) {
        return null;
    }*/

    @Override /*创建用户【用户名不能重复】*/
    public Result UserSerAdd(Users Add) {
        System.out.println(Add.getUloginname());
        Users user = userInfoMapper.getUserByName(Add.getUloginname());//首先根据登录名去查询看看有没有这个重复的如果有则不能新增

        if(user!= null){
            r=new Result(1,"登录名重复");
        }else{//如果新增的这个登录名没有重复的话再去调用新增接口
           /* long timestamp=new Date().getTime();//获取当前时间戳
            String uid=timestamp+"";//把当前时间戳变成字符串的id
            Add.setUid(uid);//添加到实体类对象中*/
            //通过MD5生成密钥
            String generate = MD5Utils.generate(Add.getUpassword());
            Add.setUpassword(generate);
            Add.setAccountbegintime(datetime);//把当前的日期添加到实体类对象中
            Integer addcount=usersMapper.UserMapAdd(Add);//调用新增接口
            if (addcount!=null && addcount >0){
                r=new Result(0,"新增成功");
            }else{
                r=new Result(1,"新增失败");
            }
        }

        return r;
    }

    @Override /*解锁锁定用户*/
    public Result UserSerUnlocklock(Users up) {
        Integer addcount=usersMapper.UserMapUnlocklock(up);
        if (addcount!=null && addcount >0){
            r=new Result(0,"请求成功");
        }else{
            r=new Result(1,"请求失败");
        }
        return r;
    }

    @Override/*编辑*/
    public Result UserSerUp(Users Up) {
        Integer up=usersMapper.UserMapUp(Up);
        System.err.println(up);
        if (up!=null && up >0){
            r=new Result(0,"修改成功");
        }else{
            r=new Result(1,"修改失败");
        }
        return r;
    }

    @Override/*删除*/
    public Result UserSerDel(Users del) {
        Integer ConDel=usersMapper.UserMapDel(del);
        System.err.println(del);
        if (ConDel!=null && ConDel >0){
            r=new Result(0,"删除成功");
        }else{
            r=new Result(1,"删除失败");
        }
        return r;
    }

    @Override/*重置密码*/
    public Result UsersSerPwdUp(Users pwdup) {
        pwdup.setPawresettime(datetime);
        Integer Conpwdup=usersMapper.UsersMapPwdUp(pwdup);
        System.err.println(Conpwdup);
        if (Conpwdup!=null && Conpwdup >0){
            r=new Result(0,"重置成功");
        }else{
            r=new Result(1,"重置失败");
        }
        return r;
    }
}
